﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Navigation;
using Project5188.BUS;
using Project5188.BUS.Operations;
using ExportToExcelTools;
using Project5188.Client.ClientCodes;
using Project5188.Client.Inspection;
using Project5188.Client.Personnel;

namespace Project5188.Client.Operations
{
    /// <summary>
    /// Interaction logic for PersonelInfoScreenPage.xaml
    /// </summary>
    public partial class PersonnelInfoPage
    {
        private CollectionViewSource _resources;

        public PersonnelInfoPage()
            : base(Extensions.PageNames.PersonnelInfoPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            BindData();
        }

        private void BindData()
        {
            var parameterValues = new ParameterValues();

            drpGender.SelectedValuePath = "ID";
            drpGender.IsSynchronizedWithCurrentItem = true;
            drpGender.ItemTemplate = Resources["comboTemplate"] as DataTemplate;
            drpGender.ItemsSource = parameterValues.GetEntityByParameterId(4);
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (((FrameworkElement)(e.OriginalSource)).Name == txtRegistryNo.Name ||
                ((FrameworkElement)(e.OriginalSource)).Name == txtSskNo.Name ||
                ((FrameworkElement)(e.OriginalSource)).Name == txtTCNo.Name)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
            }
            base.OnPreviewTextInput(e);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgPersonnelInfos.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<personnelinfo> personnelInfoDetail = new PersonnelInfos();
            insert.SetEntity(personnelInfoDetail, Extensions.PageNames.PersonnelInfoPage.ToString());
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var personnelInfos = new PersonnelInfos();
                _resources = Resources["personnelInfoViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    personnelInfos.GetEntityListByParameters(txtRegistryNo.Text, txtNameSurname.Text, txtTCNo.Text,
                                                             txtSskNo.Text, dtEntryToCompanyDate.SelectedDate,
                                                             Convert.ToInt64(drpGender.SelectedValue)), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgPersonnelInfos.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var personnelInfos = new PersonnelInfos();
                _resources = Resources["personnelInfoViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                personnelInfos.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgPersonnelInfos.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var detail = new DetailWindow();
            DetailView<personnelinfo> personnelInfoDetail = new PersonnelInfos { Entity = personnelInfoValue };
            detail.SetEntity(personnelInfoDetail, Extensions.PageNames.PersonnelInfoPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<personnelinfo> personnelInfoDetail = new PersonnelInfos();
            update.SetEntity(personnelInfoDetail, Extensions.PageNames.PersonnelInfoPage.ToString(), personnelInfoValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            DetailView<personnelinfo> personnelInfoDetail = new PersonnelInfos();
            GenerateDeleteDialog(personnelInfoDetail, personnelInfoValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgPersonnelInfos.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }

        private void BtnAddActionClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new PersonnelActionPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddIdCardInfoClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new IdCardInfoPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddCommunicationInfoClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new CommunicationInfoPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddInsuranceInfoClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new InsuranceInfoPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddDressInfoClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new DressInfoPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddDinnerExpenseClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new DinnerExpenseDetailPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddPersonnelNomineeClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new PersonnelNomineePage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddWorkOutClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new PersonnelWorkOutPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }

        private void BtnAddPersonnelInspectionClick(object sender, RoutedEventArgs e)
        {
            var personnelInfoValue = (personnelinfo)dgPersonnelInfos.SelectedItem;
            var personnelAction = new PersonnelInspectionPage(personnelInfoValue.a_ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(personnelAction);
        }
    }
}
